<template>
  <div class="register-container">
    <div class="register-box">
      <div class="register-title">
        <i class="el-icon-s-custom"></i>
        <span>用户注册</span>
      </div>
      
      <el-form 
        :model="ruleForm" 
        :rules="rules" 
        ref="ruleForm" 
        label-width="80px" 
        class="register-form"
      >
        <el-form-item label="账号名称" prop="user" class="el-username">
          <el-input 
            v-model="ruleForm.user" 
            prefix-icon="el-icon-user"
            placeholder="请输入3-16位的字母、数字"
            clearable>
          </el-input>
        </el-form-item>

        <el-form-item label="手机号码" prop="mobile" class="el-mobile">
          <el-input 
            v-model="ruleForm.mobile" 
            prefix-icon="el-icon-mobile-phone"
            placeholder="请输入11位手机号码"
            clearable>
          </el-input>
        </el-form-item>

        <el-form-item label="电子邮箱" prop="email" class="el-email">
          <el-input 
            v-model="ruleForm.email" 
            prefix-icon="el-icon-message"
            placeholder="请输入电子邮箱地址"
            clearable>
          </el-input>
        </el-form-item>

        <el-form-item label="账号密码" prop="pass" class="el-pass">
          <el-input 
            type="password" 
            v-model="ruleForm.pass"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            clearable>
          </el-input>
        </el-form-item>

        <el-form-item label="确认密码" prop="checkPass" class="el-checkpass">
          <el-input 
            type="password" 
            v-model="ruleForm.checkPass"
            prefix-icon="el-icon-key"
            placeholder="请再次输入密码"
            clearable>
          </el-input>
        </el-form-item>
      </el-form>
      <div class="btn-group">
        <el-button 
            type="primary" 
            @click="submitForm('ruleForm')"
            class="submit-btn">
            <i class="el-icon-check"></i>
            注册
          </el-button>
          <el-button 
            @click="returnForm"
            class="cancel-btn">
            <i class="el-icon-back"></i>
            返回
          </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

  export default {
    data() {
      var checkUser = (rule, value, callback) => {
        const regUser = /^[a-zA-Z0-9_-]{3,16}$/
        if (regUser.test(value)) {
          return callback()
        }
        callback(new Error('用户名不能为空'))
      }
      var checkMobile = (rule, value, callback) => {
        const regUser = /^((0\d{2,3}-\d{7,8})|(1[34578]\d{9}))$/
        if (regUser.test(value)) {
          return callback()
        }
        callback(new Error('手机号码不能为空'))
      }
      var checkEmail = (rule, value, callback) => {
        const regUser = /^([a-zA-Z0-9]+[-_]?)+@[a-zA-Z0-9]+\.[a-z]+$/
        if (regUser.test(value)) {
          return callback()
        }
        callback(new Error('邮箱不能为空'))
      }
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'))
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass')
          }
          callback()
        }
      }
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'))
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()
        }
      }
      return {
        ruleForm: {
          user: '',
          mobile: '',
          email: '',
          pass: '',
          checkPass: ''
        },
        rules: {
          user: [{ validator: checkUser, trigger: 'blur' }],
          mobile: [{ validator: checkMobile, trigger: 'blur' }],
          email: [{ validator: checkEmail, trigger: 'blur' }],
          pass: [{ validator: validatePass, trigger: 'blur' }],
          checkPass: [{ validator: validatePass2, trigger: 'blur' }]
        }
      }
    },
    methods: {
      returnForm() {
        // 返回login界面
        this.$router.push('/')
      },
      submitForm() {
        console.log(this.ruleForm)
        this.$refs.ruleForm.validate().then(()=> {
            let pwd = this.ruleForm.pass
            const formData = {
                sysUsrName: this.ruleForm.user,
                sysUsrPassword: this.$rsa.encrypt(pwd),
            }
            this.$rsa.encrypt(pwd)
            console.log(JSON.stringify(formData))
            //模拟异步请求后台接口 登录操作
            this.$axios.post('/security/sysUsr/register', JSON.stringify(formData),{
                headers: {
                    'Content-Type': 'application/json',
                }
            }).then(response => {
            console.log('登录成功:', response.data);
            alert('登录成功!');
            }).catch(error => {
            console.error('登录失败:', error);
            alert('登录失败!');
            });
        })
      }
    }
  }
  </script>

  <style scoped>
/* 重置全局样式 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.register-container {
  min-height: 100vh;
  width: 100%;
  background: linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.register-box {
  width: 400px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 15px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.08);
  backdrop-filter: blur(8px);
  animation: slideIn 0.5s ease-out;
}

.register-title {
  text-align: center;
  margin-bottom: 30px;
  color: #409EFF;
  font-size: 24px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
}

.register-title i {
  font-size: 28px;
  margin-right: 10px;
}

.register-form {
  margin-top: 30px;
  width: 100%;
}

.el-form-item__label-wrap {
  margin-left: 0px !important;
}

/* 调整表单项样式 */
:deep(.el-form-item__label) {
  margin-left: 0px !important;
  line-height: 40px;
}

:deep(.el-form) {
  display: flex;
  flex-direction: column;
}

:deep(.el-form-item) {
  display: flex;
  margin-bottom: 30px;
}

:deep(.el-form-item__content) {
  margin-left: 0px !important;
  width: calc(100% - 80px);
  flex: 1;
}

/* 调整输入框样式 */
:deep(.el-input) {
  width: 260px;
}

/* 确保每个表单项之间的间距 */
.el-form-item {
  margin-bottom: 30px;
}

/* 按钮组样式 */
.btn-group {
  margin-top: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
}

.submit-btn, .cancel-btn {
  width: 200px;
  height: 40px;
  font-size: 16px;
  justify-content: center;

  transition: all 0.3s;
}

.submit-btn:hover, .cancel-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Element UI 样式覆盖 */
:deep(.el-input__inner) {
  height: 40px;
  line-height: 40px;
  padding-left: 35px;
}

:deep(.el-form-item) {
  margin-bottom: 0;
}

:deep(.el-input__prefix) {
  left: 10px;
  line-height: 40px;
}

/* 移除 el-form-item 的默认样式 */
:deep(.btn-group.el-form-item) {
  margin: 0;
  padding: 0;
  margin-top: 30px;
}

/* 移除 el-form-item__content 的默认样式 */
:deep(.btn-group .el-form-item__content) {
  margin-left: 0 !important;
  width: 100%;
  display: flex;
  justify-content: center;
}
  </style>
  
  